<template>


  <div>

    <el-row>


      <el-col :span="12">
        <el-button @click="showEdit = true">添加</el-button>

      </el-col>
      <!--      <el-col :span="12"><div class="grid-content ep-bg-purple-dark" /></el-col>-->
    </el-row>


    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column prop="deptId" label="deptId" width="180" />
      <el-table-column prop="deptName" label="deptName" width="180" />

      <el-table-column prop="loc" label="loc" />
      <el-table-column prop="leader" label="leader" />
      <el-table-column prop="createtime" label="createtime" />
      <el-table-column prop="userid" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="deleteById(scope.row.deptId)">删除</el-button>
          <el-button link type="primary" @click="toEdit(scope.row.deptId)">修改</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination v-model:current-page="queryParam.current" :page-sizes="[3, 5, 10, 15, 20, 30, 40]"
      v-model:page-size="queryParam.size" size="default" layout="sizes, prev, pager, next" :total="total"
      @size-change="loadDataList" @current-change="loadDataList" />



    <el-dialog title="部门管理" width="600px" v-model="showEdit">

      <el-form :model="form" label-width="auto">
        <el-form-item label="部门名称">
          <el-input v-model="form.deptName" />
        </el-form-item>
        <el-form-item label="办公地点">
          <el-input v-model="form.loc" />
        </el-form-item>

        <el-form-item label="科室主任">
          <el-input v-model="form.leader" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="closeDialog()">Cancel</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


  </div>




</template>


<script lang="ts" setup name="User">

import { ref, onMounted, reactive } from 'vue'
// import { service } from "@/utils/request";
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

import { page, deleteById as del, queryById, saveOrUpdate, type PageInfo } from '@/utils/crud'

interface Dept {
  deptId?: string,
  deptName: string,
  loc: string,
  leader: string,
  createtime?: string
}


//数据

const tableData = ref<Dept[]>()
const queryParam = reactive({
  current: 1,
  size: 10,
});
const total = ref(0);
const loading = ref(false);

const form = reactive<Dept>({
  deptName: '',
  loc: '',
  leader: '',
})
const showEdit = ref(false);


function loadDataList() {
  loading.value = true;
  // 为给定 ID 的 user 创建请求
  //data  真实的数据
  page<Dept>('dept/page', queryParam).then(function (page) {
    tableData.value = page.data;
    total.value = page.total;
    loading.value = false;
  })

}

function deleteById(id: string) {
  ElMessageBox.confirm(
    '是否删除数据?',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      del('/dept/delete', id).then(function (success) {
        if (success) {
          ElMessage({
            message: '删除成功',
            type: 'success'
          })

          loadDataList();
        } else {
          ElMessage({
            message: '删除失败',
            type: 'error'
          })

        }
      })
    })

}

function save() {

  saveOrUpdate('/dept/save', form).then(function (success) {
    if (success) {
      ElMessage({
        message: '保存成功',
        type: 'success'
      })
      closeDialog();
      loadDataList();
    } else {
      ElMessage({
        message: '保存失败',
        type: 'error'
      })
    }

  })
}


function toEdit(id: string) {


  //根据主键查询 数据 放到  form 表单
  queryById<Dept>('/dept/getById', id).then((dept) => {

    showEdit.value = true;

    form.deptId = dept.deptId;
    form.deptName = dept.deptName;
    form.loc = dept.loc;
    form.leader = dept.leader;
  })

}
function closeDialog() {
  showEdit.value = false
}

onMounted(() => {
  // 获取数据
  loadDataList();
})


</script>
